﻿namespace Shell.PocoDot.Configurations
{
    /// <summary>
    /// 表示一个持久化提供器的配置信息
    /// </summary>
    public sealed class ProviderConfiguration
    {
        /// <summary>
        /// 构造一个新的实例
        /// </summary>
        /// <param name="name">当前持久化提供器的实例的名称</param>
        public ProviderConfiguration(string name)
        {
            this.Name = name;
            this.ConnectionStringOrName = name;
        }

        /// <summary>
        /// 获取此持久化提供器的名称
        /// </summary>
        public string Name { get; private set; }

        /// <summary>
        /// 获取或设置一个值, 该值指示当前实例是否在持久化层不存在时自动创建之
        /// </summary>
        public bool CreateIfNotExists { get; set; }

        /// <summary>
        /// 获取或设置持久化提供器的连接字符串或其名称
        /// </summary>
        /// <remarks>如果指定的值为持久化提供器的连接字符串的名称, 则会从应用程序配置文件中的 connectionStrings 节点读取</remarks>
        public string ConnectionStringOrName { get; set; }

        /// <summary>
        /// 获取或设置此持久化提供器的固定名称
        /// </summary>
        public string ProviderInvariantName { get; set; }

        /// <summary>
        /// 获取或设置此持久化提供器的清单令牌
        /// </summary>
        public string ProviderManifestToken { get; set; }

        /// <summary>
        /// 获取当前实例的哈希值
        /// </summary>
        /// <returns>哈希值</returns>
        public override int GetHashCode()
        {
            return this.Name.GetHashCode();
        }

        /// <summary>
        /// 决定指定的实例是否与当前实例相同
        /// </summary>
        /// <param name="obj">指定的实例</param>
        /// <returns>判决结果</returns>
        public override bool Equals(object obj)
        {
            var config = obj as ProviderConfiguration;
            if (config == null)
            {
                return false;
            }
            return config.Name == this.Name
                && config.ProviderInvariantName == this.ProviderInvariantName
                && config.ProviderManifestToken == this.ProviderManifestToken
                && config.ConnectionStringOrName == this.ConnectionStringOrName
                && config.CreateIfNotExists == this.CreateIfNotExists;
        }
    }
}
